package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.pojo.Category;
import com.itheima.reggie.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
@Slf4j
public class CategoryController {

    @Autowired
    private CategoryService service;
    @PostMapping
    public R<String> add(@RequestBody Category category){
        log.info("新增菜单:{}",category.toString());
        service.save(category);

        return R.success("添加成功");
    }
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize){

        Page<Category> p=new Page<>(page,pageSize);

        LambdaQueryWrapper<Category> lqw=new LambdaQueryWrapper();
        lqw.orderByAsc(Category::getSort);
        service.page(p,lqw);
        return R.success(p);

    }
    @PutMapping
    public R<String> update(@RequestBody Category category){

        service.updateById(category);
        return R.success("修改成功啦!");

    }

    @DeleteMapping
    public R<String> delete(Long id){
        service.remove(id);
        return R.success("删除成功了");
    }

    @GetMapping("/list")
    public R<List<Category>> list(Category category){
        log.info("type:{}",category.getType());
        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper();
        lqw.eq(category.getType()!=null,Category::getType,category.getType());
        List<Category> list = service.list(lqw);
        return R.success(list);
    }

}
